/**
 * 
 */
package fr.ants.view.panel;

import java.awt.Dimension;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.TransferHandler;

import fr.ants.controller.command.ApplyNestCommand;
import fr.ants.controller.listener.ApplyButtonListener;
import fr.ants.controller.listener.ButtonsListener;
import fr.ants.model.Nest;
import fr.ants.view.HMI;
import fr.ants.view.Selection;
import fr.ants.view.component.SliderPanel;

/**
 * <!-- begin-UML-doc --> <!-- end-UML-doc -->
 * 
 * @author Nyu
 * @generated 
 *            "UML vers Java (com.ibm.xtools.transform.uml2.java5.internal.UML2JavaTransform)"
 */
public class NestPanel extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5836706313031004225L;
	/**
	 * panel nest button.
	 */
	private final NestButtonsPanel nestButtonsPanel;

	/**
	 * button to apply the modification.
	 */
	private final JButton applyButton;

	/**
	 * life slider.
	 */
	private final SliderPanel initialAntLifespanSliderPanel;
	/**
	 * pheromone slide.
	 */
	private final SliderPanel pheromoneSliderPanel;
	/**
	 * nest size slider.
	 */
	private final SliderPanel maxNestSizeSliderPanel;
	/**
	 * queen slider.
	 */
	private final SliderPanel queenQtyPerSwarmSliderPanel;
	/**
	 * delay slider.
	 */
	private final SliderPanel delayBetweenSwarmsSliderPanel;

	/**
	 * pathfinding.
	 */
	private PathfindingPanel pathfindingPanel;

	private Nest nest;

	/**
	 * Build.
	 * 
	 * @param canvas canvas
	 * @param applyButtonListener listener for the button apply
	 * @param buttonsListener listener for the other buttons
	 * @param transferHandler for the drag and drop
	 */
	public NestPanel(final Canvas canvas,
			final ApplyButtonListener applyButtonListener,
			final ButtonsListener buttonsListener,
			final TransferHandler transferHandler) {
		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));

		nestButtonsPanel = new NestButtonsPanel(buttonsListener,
				transferHandler);
		add(nestButtonsPanel);

		// pathfinding
		pathfindingPanel = new PathfindingPanel();
		add(pathfindingPanel);

		// life
		initialAntLifespanSliderPanel = new SliderPanel(HMI.LIFE);
		initialAntLifespanSliderPanel.setMaximum(255);
		initialAntLifespanSliderPanel.setMinimum(1);
		add(initialAntLifespanSliderPanel);

		// pheromone
		pheromoneSliderPanel = new SliderPanel(HMI.PHEROMONE);
		pheromoneSliderPanel.setMaximum(255);
		pheromoneSliderPanel.setMinimum(1);
		add(pheromoneSliderPanel);

		// nest size
		maxNestSizeSliderPanel = new SliderPanel(HMI.SIZE_NEST);
		maxNestSizeSliderPanel.setMaximum(255);
		maxNestSizeSliderPanel.setMinimum(1);
		add(maxNestSizeSliderPanel);

		// queen amount slider
		queenQtyPerSwarmSliderPanel = new SliderPanel(HMI.QUEEN);
		queenQtyPerSwarmSliderPanel.setMaximum(10);
		queenQtyPerSwarmSliderPanel.setMinimum(1);
		add(queenQtyPerSwarmSliderPanel);

		// delay
		delayBetweenSwarmsSliderPanel = new SliderPanel(HMI.DELAY);
		delayBetweenSwarmsSliderPanel.setMaximum(255);
		delayBetweenSwarmsSliderPanel.setMinimum(1);
		add(delayBetweenSwarmsSliderPanel);

		applyButton = new JButton(HMI.APPLY);
		applyButton.setEnabled(false);
		applyButton.setActionCommand(HMI.APPLY_ACTION_NEST);
		applyButton.addActionListener(applyButtonListener);
		applyButtonListener.setApplyNestCommand(new ApplyNestCommand(this));
		add(applyButton);

		add(Box.createRigidArea(new Dimension(0, 150)));
	}

	/**
	 * pheromone slide.
	 * 
	 * @return pheromone value
	 */
	public int getPheromeValue() {
		return pheromoneSliderPanel.getValue();
	}

	/**
	 * nest size slider.
	 * 
	 * @return value of size
	 */
	public int getMaxNestSizeValue() {
		return maxNestSizeSliderPanel.getValue();
	}

	/**
	 * queen slider.
	 * 
	 * @return value
	 */
	public int getQueenQtyPerSwarmValue() {
		return queenQtyPerSwarmSliderPanel.getValue();
	}

	/**
	 * delay slider.
	 * 
	 * @return value delay between two expansion
	 */
	public int getDelayBetweenSwarmsValue() {
		return delayBetweenSwarmsSliderPanel.getValue();
	}

	/**
	 * life slider.
	 * 
	 * @return value life
	 */
	public int getInitialAntLifespanSliderPanelValue() {
		return initialAntLifespanSliderPanel.getValue();
	}

	/**
	 * pheromone slider.
	 * 
	 * @return value life
	 */
	public int getPheromoneSliderPanelValue() {
		return pheromoneSliderPanel.getValue();
	}

	public Nest getNest() {
		return nest;
	}

	public void init(final Nest nest) {
		this.nest = nest;
		applyButton.setEnabled(false);
		initialAntLifespanSliderPanel.setValue(nest.getInitialAntLifespan());
		pheromoneSliderPanel.setValue(nest.getPheromoneMaxQty());
		maxNestSizeSliderPanel.setValue(nest.getMaxNestSize());
		queenQtyPerSwarmSliderPanel.setValue(nest.getQueenQtyPerSwarm());
		delayBetweenSwarmsSliderPanel.setValue(nest.getDelayBetweenSwarms());
	}

	/**
	 * to enable or not the button apply.
	 * 
	 * @param true to activate otherwise false
	 */
	public void setEnabledButtonApply(final boolean b) {
		applyButton.setEnabled(b);

	}

	/**
	 * to enable or not the new nest button .
	 * 
	 * @param true to activate otherwise false
	 */
	public void setEnabledButtonNest(final boolean b) {
		nestButtonsPanel.setEnabledButtonNest(b);

	}
	
	/**
	 * to show what button is active or not
	 * 
	 * @param Selection
	 */
	public void setActiveButtonTools (Selection selection) {
		nestButtonsPanel.activeButtons(selection);
	}

	/**
	 * to show what slider is active or not
	 * 
	 * @param Selection
	 */
	public void setActiveSliders(Selection selection) {

		if (selection.equals(Selection.NEST)) {
			initialAntLifespanSliderPanel.setVisible(true);
			pheromoneSliderPanel.setVisible(true);
			maxNestSizeSliderPanel.setVisible(true);
			queenQtyPerSwarmSliderPanel.setVisible(true);
			delayBetweenSwarmsSliderPanel.setVisible(true);
			pathfindingPanel.setVisible(true);
			applyButton.setVisible(false);
		}
		else if (selection.equals(Selection.SELECTION) || selection.equals(Selection.CLICK_RIGHT)) {
				initialAntLifespanSliderPanel.setVisible(true);
				pheromoneSliderPanel.setVisible(true);
				maxNestSizeSliderPanel.setVisible(true);
				queenQtyPerSwarmSliderPanel.setVisible(true);
				delayBetweenSwarmsSliderPanel.setVisible(true);
				pathfindingPanel.setVisible(true);
				applyButton.setVisible(true);
			}
		else {
			initialAntLifespanSliderPanel.setVisible(false);
			pheromoneSliderPanel.setVisible(false);
			maxNestSizeSliderPanel.setVisible(false);
			queenQtyPerSwarmSliderPanel.setVisible(false);
			delayBetweenSwarmsSliderPanel.setVisible(false);
			pathfindingPanel.setVisible(false);
			applyButton.setVisible(false);			
		}
		
	}

}